﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OnlineStore.Business_Logic_Layer;
using OnlineStore.Data_Access_Layer;
using System.IO;

namespace OnlineStore
{
    public partial class frmProductItem : Form
    {
        private BizProduct eBizProduct;
        int nProductID = -1;

        public frmProductItem()
        {
            eBizProduct = new BizProduct();
            InitializeComponent();
        }
        
        // Open the diaglog to choose a picture.
        private void ProductImage_Click(object sender, EventArgs e)
        {
            OpenFileDialog Open = new OpenFileDialog();
            Open.Filter = "Image Files (*.bmp, *.jpg)|*.bmp;*.jpg";
            if (Open.ShowDialog() == DialogResult.OK)
            {
               ProductImage.Image = System.Drawing.Bitmap.FromFile(Open.FileName);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            // validate data...
            try
            {
                double.Parse(txtPrice.Text);
            }
            catch (Exception ex)
            {
                ex.ToString();
                MessageBox.Show("Price must be number...");
                return;
            }
            // Check product ID
            try
            {
                List<tbProduct> eListProduct = new List<tbProduct>();
                eListProduct = eBizProduct.GetProductbyKeyWord("Product ID", txtProductID.Text);
                
                if (nProductID  == -1 && eListProduct.Count > 0 ) // check in case add new
                {
                    MessageBox.Show("Product ID is existed...");
                    return;
                }
                if (eListProduct.First().ID != nProductID && eListProduct.Count > 0) // in case update
                {
                    MessageBox.Show("Product ID is existed...");
                    return;
                }
            }
            catch (Exception)
            {
                Exception ex;
                return;
            }
            try
            {
                if (btnSave.Text.Trim() == "Save")
                {
                    // Save product image to file.
                    ProductImage.Image.Save("Image\\" + txtProductID.Text.ToString().Trim() + ".jpg");
                    // Save product information.
                    eBizProduct.SaveProductItem(-1, txtProductID.Text, txtProductName.Text, cbType.Text, txtColor.Text, cbSize.Text, cbParent.Text, txtIntroduce.Text, txtProductDetail.Text, "Image\\" + txtProductID.Text.ToString().Trim() + ".jpg", txtPrice.Text, isClocked.Checked, ProductImage.Image);
                }
                if (btnSave.Text.Trim() == "Update")
                {
                    //ProductImage.Image.Save("Image\\" + txtProductID.Text.ToString().Trim() + ".jpg");
                    eBizProduct.SaveProductItem(nProductID, txtProductID.Text, txtProductName.Text, cbType.Text, txtColor.Text, cbSize.Text, cbParent.Text, txtIntroduce.Text, txtProductDetail.Text, "Image\\" + txtProductID.Text.ToString().Trim() + ".jpg", txtPrice.Text, isClocked.Checked, ProductImage.Image);
                }
                // Reload data grid.
                tabControl1.SelectedTab = tabView;
                LoadProductList();
                ResetValue();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void frmProductItem_Load(object sender, EventArgs e)
        {
            LoadProductList();
            LoadParentCombo();
        }
        // load data into data grid Product Item List on View tab
        private void LoadProductList()
        {            
            List<tbProduct> ProductList = eBizProduct.GetProductbyKeyWord("","");
            tbProduct eProduct = new tbProduct();
            dgProductList.Rows.Clear();
            for (int i = 0; i < ProductList.Count; i++)
            {
                eProduct = ProductList.ElementAt<tbProduct>(i);
                string[] row1 = new string[] {eProduct.ID.ToString(), eProduct.Product_ID.ToString(), eProduct.Product_Name.ToString(), eProduct.Type.ToString(), eProduct.Color.ToString(), eProduct.Size.ToString(), eProduct.Introduce.ToString(), eProduct.Farther.ToString()};
                dgProductList.Rows.Add(row1);
            }
        }
        private void LoadParentCombo()
        {
            cbParent.Items.Clear();
            List<tbProduct> ProductList = eBizProduct.GetProductbyKeyWord("","");
            tbProduct eProduct = new tbProduct();
            for (int i = 0; i < ProductList.Count; i++)
            {
                eProduct = ProductList.ElementAt<tbProduct>(i);
                cbParent.Items.Add(eProduct.ID);
            }
        }
        // Reload product list when user keyin search.
        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                //BizProduct eBizProduct = new BizProduct();
                List<tbProduct> ProductList = eBizProduct.GetProductbyKeyWord(cbKey.Text,txtKeyWord.Text);
                tbProduct eProduct = new tbProduct();
                dgProductList.Rows.Clear();
                for (int i = 0; i < ProductList.Count; i++)
                {
                    eProduct = ProductList.ElementAt<tbProduct>(i);
                    string[] row1 = new string[] { eProduct.ID.ToString(), eProduct.Product_ID.ToString(), eProduct.Product_Name.ToString(), eProduct.Type.ToString(), eProduct.Color.ToString(), eProduct.Size.ToString(), eProduct.Introduce.ToString(), eProduct.Farther.ToString() };
                    dgProductList.Rows.Add(row1);
                }
            }
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                tabControl1.SelectedTab = tabAdd;
                nProductID = int.Parse(dgProductList.Rows[dgProductList.SelectedCells[0].RowIndex].Cells[0].Value.ToString().Trim());
                //BizProduct eBizProduct = new BizProduct();
                tbProduct eProduct = eBizProduct.GetProductbyKeyWord("ID",nProductID.ToString()).First();
                txtProductID.Text = eProduct.Product_ID.ToString();
                txtProductName.Text = eProduct.Product_Name.ToString();
                txtPrice.Text = eProduct.Price.ToString();
                txtColor.Text = eProduct.Color.ToString();
                txtIntroduce.Text = eProduct.Introduce.ToString();
                txtProductDetail.Text = eProduct.Detail.ToString();
                cbType.Text = eProduct.Type.ToString().Trim();
                cbSize.Text = eProduct.Size.ToString().Trim();
                cbParent.Text = eProduct.Farther.ToString().Trim();        

                MemoryStream ms = new MemoryStream(eProduct.Pro_Image);
                ProductImage.Image = System.Drawing.Bitmap.FromStream(ms);

                isClocked.Checked = eProduct.Clock == 1 ? true : false;
                btnSave.Text = "   Update";
                tabAdd.Text = "   Edit";
                tabAdd.Show();
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int nResult = 0;
                nProductID = int.Parse(dgProductList.Rows[dgProductList.SelectedCells[0].RowIndex].Cells[0].Value.ToString().Trim());
                nResult = eBizProduct.DeleteProductItem(nProductID);
                if (nResult == -1)
                    MessageBox.Show("Cann't delete this product item!");
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            tabControl1.SelectedTab = tabAdd;
            tabAdd.Text = "Add";
            btnSave.Text = "Save";
            ResetValue();
            LoadParentCombo();
        }
        private void ResetValue()
        {
            nProductID = -1;
            txtColor.Text = "";
            txtIntroduce.Text = "";
            txtPrice.Text = "";
            txtProductDetail.Text = "";
            txtProductID.Text = "";
            txtProductName.Text = "";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            ResetValue();
            tabControl1.SelectedTab = tabView;
        }
    }
}
